<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use think\Request;
use think\Session;
use app\admin\model\Choice;
use app\admin\model\Fillblank;
use app\admin\model\Judge;
use app\admin\model\Shortanswer;
use app\admin\model\Paper as Mpaper;
use app\admin\controller\Fillblank as Cfillblank;
use app\admin\controller\Judge as Cjudge;
use app\admin\controller\Shortanswer as Cshortanswer;
use app\admin\controller\Single as Csingle;

class Paper extends Common
{
	// 验证失败错误信息
    protected $error = '';

	public function add(){
		//查询各种试题类型的数量
		$id = Session::get('id');
		$map['creater'] = $id;
		$map['category'] = 1;
		$singlenum = Choice::where($map)->count();
		$map['category'] = 2;
		$mulnum = Choice::where($map)->count();
		$map['category'] = 3;
		$idenum = Choice::where($map)->count();
		unset($map['category']);
		$fillnum = Fillblank::where($map)->count();
		$judgenum = Judge::where($map)->count();
		$shortnum = Shortanswer::where($map)->count();
		$this->assign('singlenum',$singlenum);
		$this->assign('mulnum',$mulnum);
		$this->assign('idenum',$idenum);
		$this->assign('fillnum',$fillnum);
		$this->assign('judgenum',$judgenum);
		$this->assign('shortnum',$shortnum);
        if(Request::instance()->isGet()){
            $id = Request::instance()->param('id');
            if($id) {
                $data = Mpaper::get($id);
                $this->assign('paper',$data);
                $this->assign('id',$id);
                return $this->fetch('edit');
            }else{
                return $this->fetch();
            }
        }
		return $this->fetch();
	}
	//生成试卷
	public function doAdd(){
		if (Request::instance()->isPost()) {
			$data = Request::instance()->post();
            if($data['id']){
                //修改(删除这一条信息)
                $paper = new Mpaper;
                $re = $paper->delPaper($data['id']);
                if(!$re){
                    $this->error('更新失败');
                }
            }
            unset($data['id']);
			//过滤试卷信息
			//试卷标题不能重复
			if(!$this->isExist($data['title'])){
				$this->error($this->getError());
			}else{
				//计算试卷的总分数
				if (isset($data['ascore']) && is_array($data['ascore'])) {
					$data['allscore'] = 0;
					foreach ($data['ascore'] as $key => $value) {
						$data['allscore'] += $value;
					}
					unset($data['ascore']);
				}else{
					$this->error('试卷分数错误');
				}
				$data['creater'] = Session::get('id');
				//根据生成方式抽提 1随机 0难易程度
				$detaildata = [];
				switch ($data['createway']) {
					case '1':
						# 抽取单选题
						$detaildata['single'] = Csingle::getSingle($data['singlenum'],1);
						# 多项
						$detaildata['mul'] = Csingle::getSingle($data['mulnum'],2);
						# 不定项
						$detaildata['ide'] = Csingle::getSingle($data['idenum'],3);
						# 填空
						$detaildata['fill'] = Cfillblank::getFill($data['fillnum']);
						# 判断
						$detaildata['judge'] = Cjudge::getJudge($data['judgenum']);
						# 简答
						$detaildata['short'] = Cshortanswer::getShort($data['shortnum']);
						break;

					default:
						# 抽取单选题
						$detaildata['single'] = Csingle::getSingle($data['singlenum'],1,$data["degree"]);
						# 多项
						$detaildata['mul'] = Csingle::getSingle($data['mulnum'],2,$data["degree"]);
						# 不定项
						$detaildata['ide'] = Csingle::getSingle($data['idenum'],3,$data["degree"]);
						# 填空
						$detaildata['fill'] = Cfillblank::getFill($data['fillnum'],$data["degree"]);
						# 判断
						$detaildata['judge'] = Cjudge::getJudge($data['judgenum'],$data["degree"]);
						# 简答
						$detaildata['short'] = Cshortanswer::getShort($data['shortnum'],$data["degree"]);
						break;
				}
//				dump($detaildata);
//                exit;
				$dpaper = new Mpaper;
				if ($dpaper->saveData($data,$detaildata)) {
					$this->success('成功','admin/Paper/getList');
				}else{
					$this->error($dpaper->getError());
				}
			}
		}else{
    		$this->error('请求不合法');
    	}
	}

	/**
     * 验证试卷标题是否存在
     * @access public
     * @param string    $title 标题
     */
	public function isExist($title){
		$this->error = '';
		if (empty($title)) {
			$this->error = '标题不为空';
		}else{
			//根据title字段查询试卷
			$paperinfo = Mpaper::getByTitle($title);
			if ($paperinfo) {
				$this->error = '试卷标题已经存在';
			}
		}
		return !empty($this->error) ? false : true;	
	}

	// 获取错误信息
    public function getError()
    {
        return $this->error;
    }
    /*
     * 试卷列表
     */
    public function getList(){
        $paper = new Mpaper();
        // 查询数据集
        $clist = $paper->getList();
//        dump($clist);
        $this->assign("clist",$clist);
        return $this->fetch('list');
    }
    /**
     * 试卷预览
     * @access public
     * @param array
     */
    public function preview(){
        if(Request::instance()->isGet()){
            $id = Request::instance()->param('id');
            $paper = new Mpaper();
            $data = $paper->getAllQusetion($id);
            //取出试题列表
            $single = [];
            if($data){
                foreach($data as $key=>$val){
                    switch($key){
                        case 'single';
                            $single['single'] = Csingle::getInfoById($val);
                            unset($data[$key]);
                            break;
                        case 'mul';
                            $single['mul'] = Csingle::getInfoById($val);
                            unset($data[$key]);
                            break;
                        case 'ide';
                            $single['ide'] = Csingle::getInfoById($val);
                            unset($data[$key]);
                            break;
                        case 'fill';
                            $fill = Cfillblank::getInfoById($val);
                            unset($data[$key]);
                            break;
                        case 'judge';
                            $judge = Cjudge::getInfoById($val);
                            unset($data[$key]);
                            break;
                        case 'short';
                            $short = Cshortanswer::getInfoById($val);
                            unset($data[$key]);
                            break;
                    }
                }
            }
//            dump($single);
            $this->assign('paper',$data);
            $this->assign('single',$single);
            $this->assign('fill',$fill);
            $this->assign('judge',$judge);
            $this->assign('short',$short);

            return $this->fetch();
        }
    }

    /**
     * 发布试卷
     * @return boole
     */
    public function publish(){
        if(Request::instance()->isAjax()){
            $id = Request::instance()->post('id');
            $paper = new Mpaper;
            // 模型更新
            $result = $paper->save([
                'is_publish'  => 1
            ],['id' => $id]);
            if($result){
                $data['code'] = 1;
                echo json_encode($data);
            }else{
                $data['code'] = 0;
                echo json_encode($data);
            }
        }
    }
    /**
     * 删除试卷
     * @return boole
     */
    public function delPaper(){
        //删除试卷及试卷详细信息及试卷在试题表的id
        if(Request::instance()->isAjax()){
            $id = Request::instance()->post('id');
            $paper = new Mpaper;
            $result = $paper->delPaper($id);
//            dump($paper->getError());
            if($result){
                $data['code'] = 1;
                echo json_encode($data);
            }else{
                $data['code'] = 0;
                echo json_encode($data);
            }
        }
    }
}